<?php

// 个人主页

namespace app\shop\controller;

use think\Session;
use think\Controller;
use app\shop\common\GuessGoods;
use app\shop\model\Area as AreaModel;
use app\shop\model\Member as MemberModel;
use app\shop\model\GoodsType as GoodsTypeModel;
use app\shop\model\ShopGoods as ShopGoodsModel;
use app\shop\model\MemberOrder as MemberOrderModel;
use app\shop\model\MemberBrowse as MemberBrowseModel;
use app\shop\model\MemberCoupon as MemberCouponModel;
use app\shop\model\MemberCollect as MemberCollectModel;
use app\shop\model\MemberAddress as MemberAddressModel;
use app\shop\model\MemberBrokerage as MemberBrokerageModel;

class Information extends Controller
{

    // 前置方法
    protected $beforeActionList = [
        'checkLogin' => ['index']
    ];


    /**
     * 会员信息主页
     * @param MemberOrderModel $memberOrder
     * @param MemberBrowseModel $browse
     * @return mixed
     */
    public function index(MemberOrderModel $memberOrder, MemberBrowseModel $browse)
    {
        // 获取会员ID
        $user_id = Session::get('member_id');

        // 会员信息
        $result = self::personalInformation($user_id);

        // 购买过的商品
        $ini = [
            'sun_member_order.member_id' => $user_id,
            'sun_member_order.status'    => 4,
        ];
        $incomplete_orders = $memberOrder->with('MemberOrderAttached')->where($ini)->field('id,member_id,status,create_time')->order(['rand()' => 'desc'])->limit(4)->select()->toArray();

        // halt($incomplete_orders);
        // 浏览过的商品
        $browses = $browse->where(['sun_member_browse.member_id' => $user_id])->with('ShopGoods')->order(['id' => 'desc'])->limit(10)->select()->toArray();

        // 猜你喜欢
        $guess_like = (new GuessGoods)->guessLike();


        return $this->fetch('', [
            'mate_head'  => '我的资料',
            'data'       => $result,
            'guess_like' => $guess_like,
            'browses'    => $browses,
            'orders'     => $incomplete_orders,

        ]);
    }


    /**
     * 基本资料
     * @param GoodsTypeModel $goodsType
     * @param MemberModel $members
     * @return mixed
     */
    public function basic(GoodsTypeModel $goodsType, MemberModel $members)
    {
        // 会员ID
        $member_id = Session::get('member_id');

        // 获取会员信息
        $item = $members::get($member_id);

        $item['username'] = substr($item['username'],0,3)."****".substr($item['username'],7,4);

        // 获取商品分类信息
        // $type_arr = $goodsType->where(['status' => 1])->field('id,parenTid,title')->select();
        // $menu = get_cate($type_arr);


        return $this->fetch('', [
            'mate_head' => '基本资料',
            'item'      => $item
        ]);
    }

    // /**
    //  * 会员基本资料
    //  * @param AreaModel $area
    //  * @param MemberAddressModel $address
    //  * @return mixed
    //  */
    // public function create(AreaModel $area, MemberAddressModel $address)
    // {
    //     // 获取会员ID
    //     $user_id = Session::get('member_id');
    //
    //     // 会员信息
    //     $result = self::personalInformation($user_id);
    //
    //     // 所在地区(一级)
    //     $area = $area->where('area_parent_id', 0)->order('area_sort desc')->field('area_id,area_name')->select();
    //
    //     // 会员地址信息
    //     $address_list = $address->where(['member_id' => $user_id])->limit(3)->select()->toArray();
    //
    //     // 修改收货地址
    //     // if (Request::instance()->has('address_id')) {
    //     //
    //     //     // 获取地址ID
    //     //     $addressId = Request::instance()->param('address_id');
    //     //     // 获取信息
    //     //     $address = (new MemberAddressModel())->where('id',$addressId)->find()->toArray();
    //     //
    //     //
    //     // }
    //
    //     return $this->fetch('', [
    //         'mate_head'    => '会员信息设置',
    //         'data'         => $result,
    //         'area'         => $area,
    //         'address_list' => $address_list
    //     ]);
    // }


    public function my_collect(MemberCollectModel $memberCollect)
    {
        // 获取会员ID
        $memberId = Session::get('member_id');

        // 收藏的商品
        $condition = [
            'sun_member_collect.member_id' => $memberId,
            'sun_member_collect.type_id'   => 1
        ];

        // 商品列表
        $good_lists = $memberCollect->with('ShopGoods')->where($condition)->paginate(8, false);

        // 条件筛选
        $ini = [
            'sun_member_collect.member_id' => $memberId,
            'sun_member_collect.type_id'   => 2
        ];

        // 店铺 该店铺商品列表
        $shop_lists = $memberCollect->with('Shop')->where($ini)->paginate(3, false);


        return $this->fetch('', [
            'mate_head'  => '我的收藏',
            'good_lists' => $good_lists,
            'shop_lists' => $shop_lists

        ]);
    }


    /**
     * 浏览记录
     * @param MemberBrowseModel $memberBrowse
     * @return mixed
     */
    public function browse(MemberBrowseModel $memberBrowse)
    {
        // 会员ID
        $member_id = Session::get('member_id');

        $data = $memberBrowse->with('ShopGoods')->where('sun_member_browse.member_id', $member_id)->order('create_time','desc')->paginate(10, false);




        return $this->fetch('', [
            'mate_head' => '我的浏览记录',
            'data'      => $data,
        ]);
    }


    /**
     * 会员信息
     * @param $user_id
     * @return array
     */
    public function personalInformation($user_id)
    {

        $result = (new MemberModel())->where(['id' => $user_id])->field('avatar,nickname,balance,integral,pay_pass,username,name,sex,is_vip')->find()->toArray();

        // 收藏商品
        $result['collect_goods'] = (int)(new MemberCollectModel)->where(['member_id' => $user_id, 'type_id' => '1'])->count();

        // 收藏店铺
        $result['collect_shop'] = (int)(new MemberCollectModel)->where(['member_id' => $user_id, 'type_id' => '2'])->count();

        // 浏览记录
        $result['browse_record'] = (int)(new MemberBrowseModel)->where(['member_id' => $user_id])->count();

        // 待付款
        $result['obligation'] = (int)(new MemberOrderModel)->where(['member_id' => $user_id, 'status' => '0'])->count();

        // 待收货
        $result['receiving'] = (int)(new MemberOrderModel)->where(['member_id' => $user_id, 'status' => '2'])->count();

        // 待评价
        $result['evaluate'] = (int)(new MemberOrderModel)->where(['member_id' => $user_id, 'status' => '3'])->count();

        // 退换货
        $result['exchange'] = (int)(new MemberOrderModel)->where(['member_id' => $user_id, 'status' => ['in', '6,7']])->count();

        // 未使用优惠券
        $result['tickets'] = (int)(new MemberCouponModel)->where(['member_id' => $user_id, 'status' => 2])->count();

        // 佣金统计
        $result['commission'] = (new MemberBrokerageModel)->where(['member_id' => $user_id])->find();


        // 判断当前时间上午下午
        $result['time_solt'] = time_solt(date('G'));

        return $result;

    }


    /**
     * 前置方法(误删)
     */
    public function checkLogin()
    {
        if (false === Session::has('member_id')) {
            $this->redirect('shop/Login/index');
        }

    }
}